package com.gz.logistics.adapter;

import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.gz.logistics.R;
import com.gz.logistics.app.Define;
import com.gz.logistics.fragment.OutingFragment;
import com.gz.logistics.library.IOrder;
import com.gz.logistics.library.listener.SignAllOrdersOfTheAssembleListener;
import com.gz.logistics.library.pojo.WillOutStorageAassemble;
import com.gz.logistics.library.pojo.WillOutStorageProdoctInfo;
import com.gz.logistics.library.util.CrashHandler;
import com.gz.logistics.util.ToastUtils;

import java.lang.ref.WeakReference;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * 预出库单适配器
 * author wanner1991
 */
public class OutingAssembleAdapter extends BaseAdapter{
    private String TAG = OutingAssembleAdapter.class.getSimpleName();
    private List<WillOutStorageAassemble> mDataList;
    private Context mContext;
    private Dialog mLoadingData;
    private SignALLCallbackInterface callback;


    public OutingAssembleAdapter(List<WillOutStorageAassemble> mDataList, Context mContext,SignALLCallbackInterface callback) {
        this.mDataList = mDataList;
        this.mContext = mContext;
        this.callback = callback;
    }

    @Override
    public int getCount() {
        return mDataList.size();
    }

    @Override
    public Object getItem(int i) {
        return mDataList.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        ViewHolder viewHolder;

        mLoadingData = new Dialog(mContext,R.style.DialogTheme);
        mLoadingData.setContentView(R.layout.loading_data_dialog2);
        mLoadingData.setCanceledOnTouchOutside(false);
        mLoadingData.setCancelable(false);
        if (view == null){
            view = LayoutInflater.from(mContext).inflate(R.layout.outing_assembles_item,null);
            viewHolder = new ViewHolder();
            viewHolder.tv_assemble_part_name = (TextView) view.findViewById(R.id.tv_assemble_part_name);
            viewHolder.tv_create_time = (TextView) view.findViewById(R.id.tv_create_time);
            viewHolder.listProducts = (LinearLayout) view.findViewById(R.id.list_products);
            viewHolder.btn_sign_all = (TextView) view.findViewById(R.id.btn_sign_all);
            view.setTag(viewHolder);
        }
        final  WillOutStorageAassemble assemble = mDataList.get(i);
        viewHolder = (ViewHolder) view.getTag();
        viewHolder.tv_assemble_part_name.setText(assemble.getStorehouse_name());
        viewHolder.tv_create_time.setVisibility(View.GONE);
        viewHolder.btn_sign_all.setTag(assemble);
        /**
         * 点击预出库中的签收其下所有订单
         * 2016-03-04
         * 点击按钮后修改状态病及时刷新主页（发送广播）
         */
        viewHolder.btn_sign_all.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                WillOutStorageAassemble aassemble2 = (WillOutStorageAassemble) view.getTag();
                callback.signAll(aassemble2.getStorehouse_id(),OutingFragment.startTime, OutingFragment.endTime);
            }
        });

        List<WillOutStorageProdoctInfo> products = assemble.getWillOutPrducts();
        viewHolder.listProducts.removeAllViews();
        for (int j = 0 ; j < products.size() ; j ++ ){

            WillOutStorageProdoctInfo product = products.get(j);
            View productItemView = LayoutInflater.from(mContext).inflate(R.layout.outing_assembles_products_item, null);
            ((TextView)productItemView.findViewById(R.id.tv_product_name)).setText(product.getProduct_name());
            ((TextView)productItemView.findViewById(R.id.tv_product_num)).setText(product.getUnit()+"×"+product.getQuantity());
            ((TextView)productItemView.findViewById(R.id.tv_product_store)).setText(product.getShop_name());

            viewHolder.listProducts.addView(productItemView);
        }



        return view;

    }

    // 当点击签收其下所有订单时，调用接口里的方法
    public interface  SignALLCallbackInterface{
        void signAll(int pre_out_number,String begin_at,String end_at);
    }

    private static class  ViewHolder{
        private TextView tv_assemble_part_name;
        private TextView tv_create_time;
        private TextView btn_sign_all;
        private LinearLayout listProducts;
    }


}
